import axios from 'axios'
import {Modal} from 'antd'
import Utils from '../utils/utils'
export default class Axios{
    //封装 请求列表（添加key值）
    static requestList(_this,url,params,isMock){
        var data={
            params:params
        };
        this.ajax({
            url,data,isMock
        }).then((data)=>{
            if(data&&data.result){
                let list=data.result.item_list.map((item,index)=>{
                    item.key=index;
                    return item;
                });
                _this.setState({
                    list,
                    pagination:Utils.pagination(data,(current)=>{
                        _this.params.page=current;
                        _this.requestList();
                    })
                });
            }
        });
    }

    //请求数据
    static ajax(options){
        let loading;
        if(options.data&&options.data.isShowLoading!==false){
            loading=document.getElementById('ajaxLoading');
            loading.style.display='block';//显示加载中
        }
        let baseApi='';
        if(options.isMock){
            baseApi='https://www.easy-mock.com/mock/5b56c2f034c1d075b3b438f4/mockapi';
        }else{
            baseApi='服务器地址';
        }

        return new Promise((resolve,reject)=>{
            axios({
                url:options.url,
                method:'get',
                baseURL:baseApi,
                timeout:5000,
                params:(options.data&&options.data.params)||''
            }).then((response)=>{
                if(options.data&&options.data.isShowLoading!==false){
                    loading=document.getElementById('ajaxLoading');
                    loading.style.display='none';
                }
                if(response.status==='200'){
                    let res=response.data;
                    if(res.code==='0'){
                        resolve(res)
                    }else{
                        Modal.info({
                            title:'提示',
                            content:res.msg
                        })
                    }
                }else{
                    reject(response.data);
                }
            })
        });
    }
}